﻿using MyCompanyName.AbpZeroTemplate.DataExporting.Excel.EpPlus;
using MyCompanyName.AbpZeroTemplate.Daybooks.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Daybooks.Exporting
{
    public class DayBookExcelExporter : EpPlusExcelExporterBase, IDayBookExcelExporter
    {
        public AbpZeroTemplate.Dto.FileDto ExportToFile(List<GetStatisticsExportDto> dtos)
        {
            return CreateExcelPackage(
                "日报统计.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add("DayBook");
                    sheet.OutLineApplyStyle = true;

                    AddHeader(
                        sheet,
                        "客户",
                        "项目",
                        "业务员",
                        "工时",
                        "占比",
                        "开始时间",
                        "结束时间"
                        );

                    AddObjects(
                        sheet, 2, dtos,
                        _ => _.Customer,
                        _ => _.Project,
                        _ => _.Name,
                        _ => _.Hours,
                        _ => _.Proportion,
                        _ => _.StartTime,
                        _ => _.EndTime
                        );

                    //Formatting cells

                    var startTime = sheet.Column(6);
                    startTime.Style.Numberformat.Format = "mm-dd-yy";

                    var endTime = sheet.Column(7);
                    endTime.Style.Numberformat.Format = "mm-dd-yy";

                    for (var i = 1; i <= 7; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }


        public AbpZeroTemplate.Dto.FileDto ExportDetailToFile(List<GetStatisticsExportDetailDto> dtos)
        {
            return CreateExcelPackage(
                "日报明细.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add("DayBookDetail");
                    sheet.OutLineApplyStyle = true;

                    AddHeader(
                        sheet,
                        "事务",
                        "客户",
                        "产品",
                        "工时",
                        "备注",
                        "日期"
                        );

                    AddObjects(
                        sheet, 2, dtos,
                        _ => _.Work,
                        _ => _.Customer,
                        _ => _.Product,
                        _ => _.ManHour,
                        _ => _.Remark,
                        _ => _.BookDay.ToString("yyyy-MM-dd")
                        );

                    var endTime = sheet.Column(6);
                    endTime.Style.Numberformat.Format = "mm-dd-yy";

                    for (var i = 1; i <= 6; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }
    }
}
